package 回溯;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class _90_子集II {
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        if (nums.length == 0) return res;
        List<Integer> temp = new ArrayList<>();
        Arrays.sort(nums);
        backtrack(nums,res,temp,0);
        return res;
    }

    private void backtrack(int[] nums, List<List<Integer>> res, List<Integer> temp, int begin) {
        res.add(new ArrayList<>(temp));
        for (int j = begin; j < nums.length; j++) {
            if (j > begin && nums[j] == nums[j - 1]) {
                continue;
            }

            temp.add(nums[j]);
            backtrack(nums,res,temp,j + 1);
            temp.remove(temp.size() - 1);
        }
    }
}
